package com.irdstudio.efp.esb.service.impl.frontsystem;

import com.irdstudio.basic.framework.core.constant.ESBConstant;
import com.irdstudio.basic.framework.core.exception.ESBException;
import com.irdstudio.basic.framework.core.util.TraceUtil;
import com.irdstudio.efp.esb.common.client.resp.EsbRespRetInfBean;
import com.irdstudio.efp.esb.common.client.resp.EsbRespServiceBean;
import com.irdstudio.efp.esb.common.client.resp.EsbRespSysHeadBean;
import com.irdstudio.efp.esb.service.bo.req.frontsystem.TellerLoginValidationReqBean;
import com.irdstudio.efp.esb.service.bo.resp.frontsystem.TellerLoginValidationRespBean;
import com.irdstudio.efp.esb.service.client.ESBClientFactory;
import com.irdstudio.efp.esb.service.client.impl.LhxESBBeanCreator;
import com.irdstudio.efp.esb.service.facade.frontsystem.TellerLoginValidationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * 柜员登录验证: <br>
 * <p>
 * 0.1:caoxin:2019/6/25 7:48: 柜员登录验证 <br>
 *
 * @author caoxin
 * @version 0.1
 * @date 2019/6/25 7:48
 * @since 2019/6/25 7:48
 */
@Service("tellerLoginValidationService")
public class TellerLoginValidationServiceImpl implements TellerLoginValidationService {
    /**
     * 日志信息
     */
    private final Logger logger = LoggerFactory.getLogger(TellerLoginValidationServiceImpl.class);
    
   /** 网贷渠道类型*/
    private static final String OCM_CHANNEL_TYPE = "75";
    
    @Override
    public TellerLoginValidationRespBean loginValidate(TellerLoginValidationReqBean tellerLoginValidationReqBean) throws ESBException {
    	// 柜员登录渠道类型
    	TraceUtil.setCnlTp(OCM_CHANNEL_TYPE);
    	// 请求外围系统状态更新
        EsbRespServiceBean esbRespServiceBean = ESBClientFactory.
                buildClient().
                withTradeNo(ESBConstant.SYSTEM_ID_FRONT_SYSTEM).
                withScene(ESBConstant.SCENE_NO_03).
                withBody(tellerLoginValidationReqBean).
                create().
                sendAndReceive();
        // 返回对象
        TellerLoginValidationRespBean tellerLoginValidationRespBean = null;
        // 判断是否成功
        Optional<EsbRespSysHeadBean> sysHead = Optional.ofNullable(esbRespServiceBean.getSysHead());
        if (sysHead.isPresent()) {
            Optional<EsbRespRetInfBean> esbRespRetInfBean = sysHead.get().getRetInfArry().stream().findFirst();
            if (esbRespRetInfBean.isPresent()) {
                String retCd = esbRespRetInfBean.get().getRetCd();
                String retMsg = esbRespRetInfBean.get().getRetMsg();
                if (ESBConstant.SUCCESS.equals(retCd)) {
                    String msg = "++++====>调用大前置系统登录验证成功<====++++";
                    logger.debug(msg);
                    tellerLoginValidationRespBean = esbRespServiceBean.getBody(TellerLoginValidationRespBean.class);
                } else {
                    String msg = "++++====>调用大前置系统登录验证失败<====++++ " + retMsg;
                    logger.error(msg);
                    throw new ESBException(msg);
                }
            }
            TraceUtil.clear();
        }
        return tellerLoginValidationRespBean;
    }
}
